package com.zxl.app.controller.goods;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zxl.app.service.GoodsService;
import com.zxl.common.Page;
import com.zxl.common.Result;
import com.zxl.common.mapper.SysGoodsMapper;
import com.zxl.common.model.pojo.SysGoods;
import com.zxl.common.model.query.goods.SearchQuery;
import com.zxl.common.model.vo.goods.GoodsDetailVo;
import com.zxl.common.model.vo.goods.SearchGoodsVo;
import com.zxl.common.model.vo.member.MemberListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zxw
 * @date 2023/3/31
 */
@RestController
@RequestMapping("/app/goods")
@Slf4j
@Api(tags = {"商品模块"})
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private SysGoodsMapper sysGoodsMapper;

    @ApiOperation("商品列表/搜索")
    @GetMapping("/search")
    public Result<List<SearchGoodsVo>> search(@Valid SearchQuery query){
        List<SearchGoodsVo> goodsVoList = goodsService.search(query);
        Page<SearchGoodsVo> page = Page.pageInfoConvert(new PageInfo(goodsVoList));
        return new Result(page);
    }

    @ApiOperation("商品详情")
    @GetMapping("/goods_detail")
    public Result<GoodsDetailVo> detail(@ApiParam(value = "商品编号",required = true)@RequestParam("goodsCode")String goodsCode){
        SysGoods sysGoods = sysGoodsMapper.selectOne(new QueryWrapper<SysGoods>()
                .eq("goods_code", goodsCode));
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        BeanUtil.copyProperties(sysGoods,goodsDetailVo);
        List<String> list = new ArrayList<>();
        String goodsDetailImage = sysGoods.getGoodsDetailImage();
        String[] split = goodsDetailImage.split(",");
        for (String s : split) {
            list.add(s);
        }
        goodsDetailVo.setGoodsDetailImage(list);
        return new Result(goodsDetailVo);

    }


}
